<template>
  <el-dialog title="新增角色" :visible="isAddVisible" width="32%" @open="open" @close="close">
    <el-form ref="form" :rules="rules" :model="form" label-width="80px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="启用">
        <el-switch v-model="form.state" :active-value="1" :inactive-value="0" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="form.description" type="textarea" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="addFn()">确 定</el-button>
      <el-button @click="$emit('update:isAddVisible', false)">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addRole } from '@/api/role'
export default {
  name: 'AddRole',
  components: {},
  props: {
    isAddVisible: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      form: {
        name: '',
        state: false,
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]

      }

    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async addFn() {
      // console.log(this.form)
      // 添加
      await addRole(this.form)
      // 提示添加倾城
      this.$message.success('添加成功！')
      // 刷新
      this.$emit('success')
      this.$emit('update:isAddVisible', false)
    },
    open() {
      this.form = {
        name: '',
        state: false,
        description: ''
      }
    },
    close() {
      this.$refs.form.clearValidate()
      console.log(1)
      this.$emit('update:isAddVisible', false)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
